﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using UnityEngine;

// Npc的人工智能
// 要用状态机实现
public class NpcAI
{
    Npc _owner;

    const float TraceStopDis = 1; // 追击停止距离

    StateMachine _machine;

    State _Idle = new State();
    State _Attack = new State();

    public bool Pause = false;

    public void Init(Npc owner)
    {
        _owner = owner;
        Pause = false;

        _machine = new StateMachine();
        _Idle.EnterCallback = OnIdleEnter;
        _Idle.LoopCallback = OnIdleLoop;
        _Idle.ExitCallback = OnIdleExit;

        _Attack.EnterCallback = OnAttackEnter;
        _Attack.LoopCallback = OnAttackLoop;

        _machine.ChangeState(_Idle);
    }


    private void OnAttackLoop()
    {
        if (!_owner.IsCastingSkill)
        {
            _machine.ChangeState(_Idle);
        }
    }

    private void OnAttackEnter()
    {
        _owner.CastSkill(1);
    }

    private void OnIdleExit()
    {
    }

    private void OnIdleLoop()
    {
        // 检测周围的敌人
        if (RoleMgr.Instance.MainRole != null && RoleMgr.Instance.MainRole.IsAlive)
        {
            _owner.CurTarget = RoleMgr.Instance.MainRole;
            _machine.ChangeState(_Attack);
        }
    }

    private void OnIdleEnter()
    {
        _owner.MoveStop();
    }



    public void Loop()
    {
        if (Pause)
        {
            return;
        }

        if (_machine != null)
        {
            _machine.Loop();
        }
    }
}
